package com.gem.config;

import com.gem.interceptor.AdminInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author 刘聪
 * Created  on 2020/10/9
 */
@Configuration
@ComponentScan("com.gem.web")
@EnableWebMvc

public class MvcConfig implements WebMvcConfigurer {

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                .addInterceptor(new AdminInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login");
    }

//    配置默认跳转


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/logout").setViewName("login");
        registry.addViewController("/tologin").setViewName("login");
    }
//    配置静态资源处理

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/asserts/**").addResourceLocations("/asserts/");
    }


//    配置视图解析器

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/pages/", ".jsp");
    }

//    配置文件上传解析器
    @Bean
    public CommonsMultipartResolver commonsMultipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxInMemorySize(5242880);
        return commonsMultipartResolver;
    }
}
